package com.red.battleworm.element {
	import flash.events.MouseEvent;	
	
	import org.cove.ape.*;	
	
//	import com.red.battleworm.AppMain;	
	import com.red.battleworm.model.MapProxy;

	import flash.utils.getTimer;
	import flash.display.MovieClip;
	
	import com.red.battleworm.AppMain;	

	/**
	 * @author zhangs
	 */
	public class Player extends AbstractElement {
		public static var LEFTSPEED : Vector = new Vector(-3, 0);
		public static var RIGHTSPEED : Vector = new Vector(3, 0);
		private static var WIDTH : Number = 8;
		private static var HEIGHT : Number = 20;
		private static var LARGESTDISTANCE : Number = Number.MAX_VALUE;

		public var arrow_rotation : Number;
		public var scale : Number;
		public var isControl : Boolean;
		
		private var arrow : MovieClip;
		private var arrow_length : Number;
		private var checkTime_replay : Number;
		private var action_steps : Array;
		private var lastDistance : Number;
		
		public function Player(birth : Object) {
			super();
			
			var width : Number = Math.abs(birth.width * Math.cos(birth.rotation));
			var height : Number = Math.abs(birth.height * Math.sin(birth.rotation));
			var x0 : Number = birth.x - width / 2;
			var x1 : Number = birth.x + width / 2;
			var y0 : Number = birth.y - height / 2;
			var y1 : Number = birth.y + height / 2;
			body = new RectangleParticle(x0+Math.random()*(x1-x0), y0+Math.random()*(y1-y0)-HEIGHT/2, WIDTH, HEIGHT);
//			body = new CircleParticle(x0+Math.random()*(x1-x0), y0+Math.random()*(y1-y0)-HEIGHT/2, WIDTH);
			body.setStyle(0, MapProxy.COLOR, 1, MapProxy.COLOR);
			body.friction = 1;
			addParticle(body);
			
			auto = false;
			
			arrow = new MC_Arrow();
			arrow_length = arrow.width;
			AppMain.stage.addChild(arrow);
			
			setSpeed(NONESPEED);
			arrow.visible = true;
			checkTime = getTimer();
			clearActions();
		}

		override public function setSpeed(speed : Vector) : void {
			trace(speed);
			super.setSpeed(speed);
			
			if(isControl && speed==NONESPEED) {
				action_steps.push({speed:this.speed, position:body.position});
				trace('add:\t', this.speed, body.position, action_steps.length);
			}
			
			if (isControl && speed==NONESPEED) {
				arrow.visible = true;
			} else {
				arrow.visible = false;
			}
		}

		override public function run() : void {
			var action : Object;
			if(!isControl) {
				if(action_steps.length == 0) {
					clearActions();
					return;
				}
				action = action_steps[0];
				if(speed != action.speed) {
//					trace(speed, action.speed, action.position, action_steps.length);
					setSpeed(action.speed);
				}
			}
			arrow.x = body.px;
			arrow.y = body.py;
			/*
			var v : Vector = body.velocity;
			v.x = speed.x;
//			trace(checkTime, getTimer());
			if (v.magnitude() >= CHECKMINDISTANCE) {
				checkTime = getTimer();
			}
			if(speed.magnitude()==0 && v.magnitude()<CHECKMINDISTANCE && getTimer()>=checkTime+100) {
				body.fixed = true;
			} else {
				body.velocity = v;
			}
			 * 
			 */
			super.run();
			if(!isControl) {
				action = action_steps[0];
//				trace(lastDistance, body.position.distance(action.position));
				var d : Number = body.position.distance(action.position);
				if(d >= lastDistance) {
					if(d<=speed.magnitude()*2 || getTimer()>=checkTime_replay+100) {
//						trace('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
						resetLastDistance();
						action_steps.shift();
						body.position = action.position;
//						trace('length:\t', action_steps.length);
						if(action_steps.length == 0) {
							setSpeed(NONESPEED);
							clearActions();
						}
					}
				} else {
					checkTime_replay = getTimer();
				}
				lastDistance = d;
			}
		}
		
		private function resetLastDistance() : void {
//			count_replay = 0;
			checkTime_replay = getTimer();
			lastDistance = LARGESTDISTANCE;
		}
		
		public function clearActions() : void {
			action_steps = [body.position];
			isControl = true;
			resetLastDistance();
		}

		public function playActions() : void {
			body.position = action_steps.shift();
			isControl = false;
		}
		
		public function updateArrow(evt : MouseEvent) : void {
//			trace(evt.stageX, evt.stageY, body.px, body.py);
			var d : Number = Math.sqrt((evt.stageX-body.px)*(evt.stageX-body.px)+(evt.stageY-body.py)*(evt.stageY-body.py));
			scale = d / arrow_length;
			if (scale >= 1) {
				scale = 1;
			} else if (scale <= 0.3) {
				scale = 0.3;
			}
			if(evt.stageX == body.px) {
				if (evt.stageY >= body.py) {
					arrow_rotation = 90;
				} else {
					arrow_rotation = -90;
				}
			} else {
				var r : Number = Math.atan2((evt.stageX-body.px), (evt.stageY-body.py));
//				trace(r);
				arrow_rotation = 90-r/Math.PI*180;
			}
			arrow.scaleX = arrow.scaleY = scale;
			arrow.rotation = arrow_rotation;
		}
	}
}
